package IO流;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author:谢君臣
 * @Date:2021/8/6-14:37
 * @version:1.0
 * @function:
 */
public class TestStream {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("张三","10"));
        list.add(new Person("张三","1"));
        list.add(new Person("张三","2"));
        list.add(new Person("张三","2"));
        list.add(new Person("张三","3"));
        list.add(new Person("张三","4"));
        list.add(new Person("张三一","5"));
        list.add(new Person("张三一","6"));
        list.add(new Person("张三一","7"));
        list.add(new Person("张三一","8"));
        System.out.println("---------distinct 根据equals和hashCode去重--------------");
        list.stream().distinct().forEach(System.out::println);
        System.out.println("---------filter 条件过滤--------------");
        list.stream().filter(e->e.getName().length()>2).forEach(System.out::println);
        System.out.println("---------filter 模糊匹配 条件过滤--------------");
        list.stream().filter(e -> e.getName().contains("一")).forEach(System.out::println);
        System.out.println("-----------skip 丢掉前N个------------");
        list.stream().skip(2).forEach(System.out::println);
        System.out.println("-----------map------------");
        list.stream().map(Person::getName).forEach(System.out::println);
        System.out.println("-----------sorted------------");
        list.stream().sorted(Comparator.comparingInt(Person::getAge)).forEach(System.out::println);
        System.out.println("-----------sorted------------");
        list.stream().sorted((e1,e2)->Integer.compare(e2.getName().length(),e1.getName().length())).forEach(System.out::println);
        System.out.println("-----------findFirst------------");
        System.out.println(list.stream().sorted((e1, e2) -> Integer.compare(e2.getName().length(), e1.getName().length())).findFirst());
        System.out.println("-----------max------------");
        System.out.println(list.stream().max(Comparator.comparingInt(Person::getAge)).get());
        System.out.println("-----------reduce sum age------------");
        System.out.println(list.stream().map(Person::getAge).reduce(0, Integer::sum));
        System.out.println(list.stream().reduce((p1,p2)-> new Person(String.valueOf(p1.getAge()+p2.getAge()))).get().getAge());
        System.out.println("-----------collect toList------------");
        list.stream().map(Person::getName).collect(Collectors.toList()).forEach(System.out::println);

        //ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
        Map<String, List<Person>> collect = list.stream().collect(Collectors.groupingBy(Person::getName));
        List<Person> people = new ArrayList<>();
        collect.forEach((k,v)->people.add(v.stream().max(Comparator.comparingInt(Person::getAge)).get()));
        people.stream().forEach(System.out::println);

        Map<String, Person> collect1 = list.stream().collect(Collectors.toMap(Person::getName, person -> person, (a, b) -> Integer.compare(a.getAge(), b.getAge()) >= 0 ? a : b));
        System.out.println("-------------map-------------");
        Stream.of(collect1.keySet()).forEach(System.out::println);
        System.out.println("-------------tolist-------------");
        List<Person> list1 = list.stream().filter(e->e.getName().contains("一")).collect(Collectors.toList());
        for (Person person : list1) {
            System.out.println(person);
        }
    }
}
class Person{
    private String name;
    private String pwd;
    private int age;

    public Person(int age) {
        this.age = age;
    }

    public Person(String pwd) {
        this.pwd = pwd;
    }

    public Person(String name, String pwd) {
        this.name = name;
        this.pwd = pwd;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return Integer.parseInt(getPwd());
    }

    public String getPwd() {
        return pwd;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name) &&
                Objects.equals(pwd, person.pwd);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, pwd);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}
